package com.wits.platform.entity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;


import com.wits.platform.common.constants.SysTabConstants;
@Entity
@Table(name = "wits_role")
public class Role extends BaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4554281871167981308L;

	public Role() {

	}

	public Role(String roleId, String roleName, String checked) {
		this.roleId = roleId;
		this.roleName = roleName;
		this.checked = checked;
	}

	private String roleId;

	private String roleName;

	private String roleDescription;

	private String isEnable = SysTabConstants.SYSTAB_D_TNF_F;

	private String checked;

	private Set<Menu> menus = new HashSet<Menu>();

	private List<Resource> resources = new ArrayList<Resource>();

	private List<User> users;

	@Id
	@Column(name = "role_id", nullable = false,length=36)
	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	@Column(name = "role_name", length = 50)
	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	@Column(name = "role_description", length = 255)
	public String getRoleDescription() {
		return roleDescription;
	}

	public void setRoleDescription(String roleDescription) {
		this.roleDescription = roleDescription;
	}

	@Column(name = "is_enable", length = 1)
	public String getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(String isEnable) {
		this.isEnable = isEnable;
	}

	@ManyToMany(fetch = FetchType.LAZY,cascade=CascadeType.REFRESH)
	@JoinTable(name = "wits_mmr_role_menu", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "mu_id"))
	public Set<Menu> getMenus() {
		return menus;
	}

	public void setMenus(Set<Menu> menus) {
		this.menus = menus;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "wits_mmr_role_resource", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "rsc_id"))
	public List<Resource> getResources() {
		return resources;
	}

	public void setResources(List<Resource> resources) {
		this.resources = resources;
	}

	@ManyToMany(mappedBy = "roles", fetch = FetchType.LAZY)
	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	@Transient
	public String getChecked() {
		return checked;
	}

	public void setChecked(String checked) {
		this.checked = checked;
	}

	private static JsonConfig jsonConfig = new JsonConfig();

	@Transient
	protected JsonConfig getJsonConfig() {
		return jsonConfig;
	}

	static {
		jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
			@Override
			public boolean apply(Object arg0, String name, Object arg2) {
				// TODO Auto-generated method stub
				if (name.equals("menus")
						|| name.equals("resources") || name.equals("users")
						|| name.equals("permissions")
						|| name.equals("jsonConfig") || name.equals("dateCre")
						|| name.equals("dateCre")) {
					return true;
				}
				return false;
			}
		});
	}

	@Transient
	public List<String> getPermissions() {
		List<String> permission = new ArrayList<String>();
		for (Resource resource : resources) {
			permission.add(resource.getRscPerm());
		}
		return permission;
	}

	public String toString() {
		return "{roleId:\"" + this.roleId + "\",roleName:\"" + this.roleName
				+ "\",roleDescription:\"" + this.roleDescription
				+ "\",isEnable:\"" + this.isEnable + "\"}";
		// return JSONObject.fromObject(this,jsonConfig).toString();
	}

	@Override
	public String display() {
		return this.roleName;
	}
	
	
	public void removeMenu(Menu menu){
		if(menus.contains(menu)){
			menus.remove(menu);
		}
	}
	
	
	public void removeResource(Resource resource){
		if(resources.contains(resource)){
			resources.remove(resource);
		}
	}

}
